package com.mes.web.controller.lc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.lc.ProductGoku;
import com.mes.entity.lc.ProductStock;
import com.mes.entity.yzl.OutLib;
import com.mes.entity.yzlExtend.ExtendProContactOrder;
import com.mes.service.lc.ProductGokuService;
import com.mes.service.lc.ProductStockService;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;

/**
 * 产品出库Controller
 * @ClassName:ProductGokuController
 * @Description:
 * @Author LC
 * @Date 2018年1月31日下午4:15:07
 * @version 1.0
 */
@Controller
@RequestMapping("/progk")
public class ProductGokuController {

	@Autowired
	ProductGokuService	productGokuService;
	@Autowired
	ProductStockService	   productStockService;  //产品
	
	/**
	 * 多条件分页查询
	 * @param stockfy
	 * @param response
	 * @param limit
	 * @param offset
	 * @throws Exception
	 */
	@RequestMapping("/selectfypgk")
	public @ResponseBody void selectfypgk(
			ProductGoku productGoku,HttpServletResponse response,
			@RequestParam(value = "limit", required = false) String limit,
			@RequestParam(value = "offset", required = false) String offset)
			throws Exception {
		
			productGoku.setPage(Integer.parseInt(offset) / Integer.parseInt(limit));
			
			// 每页条数
			productGoku.setPageSize(Integer.parseInt(limit));
	
			if (StringUtil.isEmpty(productGoku.getLpodd())) {
				productGoku.setLpodd(null);
			}//出库单号
			if (StringUtil.isEmpty(productGoku.getLppici())) {
				productGoku.setLppici(null);
			}//出库批次
	
			// 物料
			List<ProductGoku> pgklist = productGokuService.selectfypgk(productGoku);
			int a = productGokuService.selectfypgkcont(productGoku);
			JsonConfig config = new JsonConfig();
			JSONObject jsonObject = new JSONObject();
			JSONArray jsonArray = JSONArray.fromObject(pgklist, config);
			jsonObject.put("rows", jsonArray);
			jsonObject.put("total", a);
			ResponseUtil.write(response, jsonObject);
	}
	
	
	/**
	 * PDA接口      查询所有未出库的销售出库单
	 */
	@RequestMapping("/selectProcurementall")
	@ResponseBody
	public Map<String,Object> selectProcurementall(HttpServletResponse response, HttpServletRequest request) throws Exception{
		List<OutLib> list = productGokuService.selectoutliball();
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("success", "1");
		map.put("data",list);
		return map;
	}
	
	
	/**
	 * PDA 接口，拿到一个销售出库单id，然后给出对应的具体信息
	 */
	@RequestMapping("/selectPcoall")
	public void selectPcoall(Long ids, HttpServletResponse response, HttpServletRequest request)
			throws Exception {

		//根据收货单id查询对应的具体信息
		List<ExtendProContactOrder> list = productGokuService.selectPcoall(ids);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", "1");
		jsonObject.put("data", list);
		ResponseUtil.write(response, jsonObject);
		
	}
	
	
	/**
	 * PDA  开始出库
	 * @param ids 出库单主键
	 * @param suprname 客户
	 * @param number 出库单编号
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping("/savepgk")
	public void savemck(Long ids,String suprname,String number,HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		
		//根据收货单id查询对应的具体信息
		List<ExtendProContactOrder> list = productGokuService.selectPcoall(ids);
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd");
		String nowtime = dateFormat.format(now);
			
		if(list.size() > 0){
			//退货之前先查询库存够不够退的(必须是入库过的物料)
			//拿到所有产品库存
			List<ProductStock> productStock = productStockService.selectProductStock();
			//定义q 是否库存数量小于出货数量
			int q = 0;
			String qq = "该产品的库存数量不足出库：";
			
			for(int i = 0 ; i < list.size() ; i ++){
				//定义P  看是否库存中有该物料
				//产品出库编号
				String a = list.get(i).getProduct().getNum();
				for(int j = 0 ; j < productStock.size() ; j ++){
					
					//如果编号相等
					if(a.equals(productStock.get(j).getProduct().getNum())){
						//判断数量是否一样
						//退货数量
						Long pcmnum = Long.valueOf(list.get(i).getOutQ());
						//库存数量
						Long mssnum = Long.valueOf(productStock.get(j).getNum());
						if(mssnum < pcmnum){
							q++;
							qq=qq+a+",";
						}
					}
				}
			}
			
			if(q > 0){
				System.err.println();
			}else{
				int a = 0 ;
				for(ExtendProContactOrder  pgk : list){
					 a = productGokuService.savepgk(pgk,suprname,number,nowtime);
				}
				//修改资材入库单状态
				if(a > 0){
					productGokuService.updateoutlib(ids);
				}
				
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("success", "1");
				jsonObject.put("data",a);
				ResponseUtil.write(response, jsonObject);
			}
		}
			
	}
		
}
